package charts.series.dots ;

import flash.display.Sprite;
import charts.series.Element;
import flash.display.BlendMode;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Point;

class PointDotBase extends Element {
	
	var radius: Int;
	var color: Int;
	
	public function new ( index: Int, style:Properties ) {
		
		super();
		this.is_tip = false;
		this.visible = true;
		
		// line charts have a value and no X, scatter charts have
		// x, y (not value): radar charts have value, Y does not 
		// make sense.
		if( !style.has('y') )
			style.set('y', style.get('value'));
	
		this._y = style.get('y');
		
		// no X passed in so calculate it from the index
		if( !style.has('x') )
		{
			this.index = this._x = index;
		}
		else
		{
			//tr.aces( 'x', style.get('x') );
			this._x = style.get('x');
			this.index = -1000000; // Math.NEGATIVE_INFINITY;
		}
		
		this.radius = style.get('dot-size');
		this.tooltip = this.replace_magic_values( style.get('tip') );
		
		if ( style.has('onclick') )
			this.set_onclick( style.get('onclick') );
		
		//
		// TODO: fix this hack
		//
		if ( style.has('axis') )
			if ( style.get('axis') == 'right' )
				this.right_axis = true;

	}
	
	public override function resize ( sc:ScreenCoordsBase ): Void {
		
		if ( this.index != Math.NEGATIVE_INFINITY ) {

			var p:flash.geom.Point = sc.get_get_x_from_pos_and_y_from_val( this.index, this._y, this.right_axis );
			this.x = p.x;
			this.y = p.y;
		}
		else
		{

			//
			// Look: we have a real X value, so get its screen location:
			//
			this.x = sc.get_x_from_val( this._x );
			this.y = sc.get_y_from_val( this._y, this.right_axis );
		}
		
		// Move the mask so it is in the proper place also
		// this all needs to be moved Into the base class
		if (this.line_mask != null)
		{
			this.line_mask.x = this.x;
			this.line_mask.y = this.y;
		}
	}
	
	public override function set_tip( b:Bool ):Void {
		//this.visible = b;
		if( b ) {
			this.scaleY = this.scaleX = 1.3;
			this.line_mask.scaleY = this.line_mask.scaleX = 1.3;
		}
		else {
			this.scaleY = this.scaleX = 1;
			this.line_mask.scaleY = this.line_mask.scaleX = 1;
		}
	}
	
	//
	// Dirty hack. Takes tooltip text, and replaces the #val# with the
	// tool_tip text, so noew you can do: "My Val = $#val#%", which turns Into:
	// "My Val = $12.00%"
	//
		function replace_magic_values( t:String ): String {
		
		t = StringTools.replace(t,'#val#', NumberUtils.formatNumber( this._y ));
		
		// for scatter charts
		t = StringTools.replace(t,'#x#', NumberUtils.formatNumber(this._x));
		t = StringTools.replace(t,'#y#', NumberUtils.formatNumber(this._y));
		
		// debug the dots sizes
		t = StringTools.replace(t,'#size#', NumberUtils.formatNumber(this.radius));
		return t;
	}
	
	function calcXOnCircle (aRadius: Float, aDegrees: Float): Float
	{
		return aRadius * Math.cos(aDegrees / 180 * Math.PI);
	}
	
	function calcYOnCircle (aRadius: Float, aDegrees: Float): Float
	{
		return aRadius * Math.sin(aDegrees / 180 * Math.PI);
	}
	
}

